import Vue from "vue";
export function checkFile(fileList,accept,singleSize,limit){
    if(Object.keys(fileList).length>limit){
        Vue.prototype.$message.error(`文件数量超出限制,最多只能上传${limit}个`);
        return false;
    }
    try {
        Object.keys(fileList).forEach(item=>{
            const file=fileList[item];
            checkExt(file,accept);
            checkSingleSize(file,singleSize);
        })
    }catch (e) {
        return false;
    }
    return true;
}
export function checkExt(file,accept){
    const fileNameArr=file.name.split('.');
    const fileExt=fileNameArr[fileNameArr.length-1].toLowerCase();
    accept=accept===null?[]:accept;
    console.log(accept,fileExt);
    if(accept.length>0&&!accept.includes(fileExt)){
        Vue.prototype.$message.error(`${file.name},上传文件的格式不允许`);
        throw new Error();
    }
}
export function checkSingleSize(file,singleSize){
    if(file.size>singleSize){
        Vue.prototype.$message.error(`${file.name},上传的文件大小超过限制，单个文件允许上传大小为${(singleSize/1024/1024)}M`);
        throw new Error();
    }
}
